package cn.jetbrain.netbeans.pdfviewer;

import java.awt.BorderLayout;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

/**
 * Thumbs View of PDF
 *
 */
public class ThumbView extends View {

    private static ThumbView instance = new ThumbView();
    private static final String PREFERRED_ID = "ThumbView";

    public static synchronized ThumbView findInstance() {
        return instance;
    }

    @Override
    public void setPDFViewer(PDFViewer v) {
        if (view == null || (!view.getName().equals(v.getName()))) {
            view = v;
            removeAll();
            setLayout(new BorderLayout());
            add(v.thumbsPane, BorderLayout.CENTER);
            revalidate();
        }
    }

    private ThumbView() {
        setName(NbBundle.getMessage(PDFDataLoader.class, "ThumbView_name"));
        setIcon(Utilities.loadImage("cn/jetbrain/netbeans/pdfviewer/view/res/thumb.png"));
        setFocusable(true);
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    @Override
    protected void componentClosed() {
        if (force) {
            force = false;
        } else {
            if (view != null) {
                if (view.thumbButton.isSelected()) {
                    view.thumbButton.setSelected(false);
                }
                if (view.outlineButton.isSelected()) {
                    OutlineView.findInstance().requestActive();
                } else {
                    if (view.old.isOpened()) {
                        view.old.requestActive();
                    }
                }
                view.requestActive();
            }
        }
        super.componentClosed();
    }
}